package com.carefree.bookmall.controller;

import com.carefree.bookmall.common.vo.BookQueryVo;
import com.carefree.bookmall.common.vo.PageVO;
import com.carefree.bookmall.entity.BookType;
import com.carefree.bookmall.exception.CustomException;
import com.carefree.bookmall.service.AliCloudService;
import com.carefree.bookmall.service.BookService;
import com.carefree.bookmall.service.BookTypeService;
import com.carefree.bookmall.service.ShortMessageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:IndexController
 * @Description:
 * @Author Tengbin
 */
@Controller
@RequestMapping("/bookmall")
public class IndexController {

    @Resource
    private BookService bookService;

    @Resource
    private BookTypeService bookTypeService;

    @Resource
    private AliCloudService aliCloudService;

    @Resource
    private ShortMessageService shortMessageService;


    @RequestMapping("/index")
    public String indexPage(HttpServletRequest request) throws CustomException {
        List<BookQueryVo> books = bookService.queryBookByRecommendType(1, 1, 8);
        if (! books.isEmpty()) {
            request.setAttribute("scrollBook", books);
        }
        // 热销
        List<BookQueryVo> hotList = bookService.queryBookByRecommendType(2, 1, 8);
        request.setAttribute("hotList", hotList);

        // 新品
        List<BookQueryVo> newList = bookService.queryBookByRecommendType(3, 1, 8);
        request.setAttribute("newList", newList);

        // 分类
        List<BookType> bookTypes = bookTypeService.queryBookTypes();
        request.setAttribute("bookTypes", bookTypes);

        return "index";
    }

    /**
     * 搜索
     *
     * @param keyword
     * @param request
     * @return
     */
    @GetMapping("/search")
    public String searchBook(int pageNum, String keyword, HttpServletRequest request) throws UnsupportedEncodingException {
        PageVO pageVO = bookService.searchBooksByKeyword(keyword, pageNum, 8);

        if (pageVO.getTotalPage() == 0) {
            pageVO.setTotalPage(1);
            pageVO.setPageNumber(1);
        } else {
            if (pageNum >= pageVO.getTotalPage() + 1) {
                pageVO = bookService.searchBooksByKeyword(keyword, pageVO.getTotalPage(), 8);
            }
        }
        request.setAttribute("page", pageVO);
        request.setAttribute("keyword", URLEncoder.encode(keyword, "utf-8"));
        return "book_search";
    }


    /**
     * 图片上传
     *
     * @param request
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public String uploadBookImages(HttpServletRequest request) throws CustomException {

        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;

        Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();

        if (fileMap.containsKey("coverUpload")) {
            MultipartFile file = multipartHttpServletRequest.getFile("coverUpload");
            return aliCloudService.uploadImages(file, "cover");

        } else if (fileMap.containsKey("image1Upload")) {
            MultipartFile file = multipartHttpServletRequest.getFile("image1Upload");
            return aliCloudService.uploadImages(file, "image1");

        } else if (fileMap.containsKey("image2Upload")) {
            MultipartFile file = multipartHttpServletRequest.getFile("image2Upload");
            return aliCloudService.uploadImages(file, "image2");
        }

        return "上传失败！";
    }

    @PostMapping("/sms/send")
    @ResponseBody
    public boolean sendShortMessageCode(String phone) {
        return shortMessageService.sendMessageByMobileNumber(phone, 6);
    }

    /**
     * 关于页面
     *
     * @return
     */
    @GetMapping("/about")
    public String aboutPage() {
        return "about";
    }

}
